package org.example;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HolidayRequestTest {

    private ProcessEngine processEngine;

    @Before
    public void before(){
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/api_gateway?serverTimezone=UTC")
                .setJdbcUsername("root")
                .setJdbcPassword("123456")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        processEngine = cfg.buildProcessEngine();
    }

    @Test
    public void test000(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("test.bpmn20.xml")
                .deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        System.out.println("Found process definition : " + processDefinition.getName());
    }

    @Test
    public void test001(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> variables = new HashMap<>();
        variables.put("employee", "employee");
        variables.put("nrOfHolidays", "nrOfHolidays");
        variables.put("description", "description");
        ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
//        ProcessInstance processInstance2 = runtimeService.startProcessInstanceById("holidayRequest:7:22503", variables);
    }

    @Test
    public void test002(){
        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
//        List<Task> tasks = taskService.createTaskQuery().active().taskCandidateOrAssigned("employee").list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (Task task : tasks) {
            Map<String, Object> variables = taskService.getVariables(task.getId());
            System.out.println(variables.toString());
            variables = new HashMap<>();
            variables.put("approved", true);
            taskService.complete(task.getId(), variables);
        }
    }

    @Test
    public void test003(){
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricActivityInstance> activities =
                historyService.createHistoricActivityInstanceQuery()
                        .processInstanceId("40001")
                        .finished()
                        .orderByHistoricActivityInstanceEndTime().asc()
                        .list();

        for (HistoricActivityInstance activity : activities) {
            System.out.println(activity.getActivityId() + " took "
                    + activity.getDurationInMillis() + " milliseconds");
        }
    }

    @Test
        public void test004(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId("52501").singleResult();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId("flow_xdv41gz2:1:52504").latestVersion().singleResult();
        Map<String, Object> variables = new HashMap<>();
        variables.put("aaa", "aaa");
        variables.put("bbb", "bbb");
        variables.put("ccc", "ccc");
        runtimeService.startProcessInstanceById("flow_xdv41gz2:1:52504",variables);
        System.out.println();
    }
}
